#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QFile>
#include<QTextStream>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;

}

void MainWindow::GetHostNameAhdIPINfo()//获取主机名称和IP地址
{
    QString  StrLocalHostIpAddress=QHostInfo::localHostName();
    ui->lineEdit->setText(StrLocalHostIpAddress);

    //根据主机名获取IP地址
    QString StrLocalIpAddress="";
    QHostInfo hostinfo=QHostInfo::fromName(StrLocalHostIpAddress);
//    QList<QHostAddress> QHostInfo::addresses() const
    QList<QHostAddress> address1=hostinfo.addresses();
    if(!address1.isEmpty())
    {
        for(QHostAddress addresshost:address1)
        {
            if(addresshost.protocol()==QAbstractSocket::IPv4Protocol)
            {
                StrLocalIpAddress=addresshost.toString();
            }
        }
    }


    ui->lineEdit_2->setText(StrLocalIpAddress);


}



//获取主机名称和IP地址
void MainWindow::on_pushButton_clicked()
{
    GetHostNameAhdIPINfo();
}
//获取主机所有信息
void MainWindow::on_pushButton_2_clicked()
{
    QString strTemp="";

    //返回主机所找到的所有的网络接口的列表
    QList<QNetworkInterface> netlist=QNetworkInterface::allInterfaces();
    for(QNetworkInterface interfaces:netlist)
    {
        strTemp=strTemp+"设备名称："+interfaces.name()+"\n"; // 获取设备名称
        strTemp=strTemp+"硬件地址："+interfaces.hardwareAddress()+"\n"; // 获取硬件地址
        QList<QNetworkAddressEntry> entrylist=interfaces.addressEntries(); // 遍历每一个IP地址对应信息
        for(QNetworkAddressEntry etry:entrylist)
        {
            strTemp=strTemp+"IP地址："+etry.ip().toString()+"\n";
            strTemp=strTemp+"子网掩码："+etry.netmask().toString()+"\n";
            strTemp=strTemp+"广播地址："+etry.broadcast().toString()+"\n";

        }
    }
     QMessageBox::information(this,"主机所有信息",strTemp,QMessageBox::Yes);
     QFile file("D:\\job\\qt\\work\\me\\day5\\1.txt");
     file.open(QIODevice::WriteOnly);
     QTextStream stream(&file);
     stream<<strTemp;
     file.close();
}































